<template>
  <div>
    <h1 @click="findAll">分类列表</h1>
    <el-button type="primary" size="min" @click="preSave" style="margin-left: 50%">添加</el-button>
    <el-table
      :data="category"

      style="width: 60%;margin-left: 20%">

      <el-table-column
        label="id"
        prop="id">
      </el-table-column>
      <el-table-column
        label="名称"
        prop="cname">
      </el-table-column>

      <el-table-column
        align="right">
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="handleEdit(scope.row.id,scope.row.cname)">Edit
          </el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.row.id)">Delete
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-form :model="categoryForm"
             ref="categoryForm" label-width="100px"
             style="width: 60%;margin-left: 15%;margin-top: 60px"
    >
      <el-form-item
        label="名称"
        prop="cname"
      >
        <el-input v-model="categoryForm.cname"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm" v-if="isEdit">修改</el-button>
        <el-button type="primary" @click="saveCategory" v-if="!isEdit">添加</el-button>
        <el-button @click="resetForm()">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "Category",
  data() {
    return {
      isEdit: true,
      category: {
        id: '',
        cname: ''
      },
      categoryForm: {
        id: '',
        cname: ''
      }
    }
  },
  created() {
    this.findAll()
  },
  methods: {
    //初始化添加
    preSave() {
      this.isEdit = false
      this.categoryForm.cname = ''
    },
    //添加分类
    saveCategory() {
      this.$axios({
        method: 'post',
        url: '/admin/saveCategory',
        data: this.categoryForm
      }).then(res => {
        if (res.data.code == 2000) {
          alert('添加成功')
          this.findAll()
        } else {
          alert('添加失败')
        }
      })
    },
    handleEdit(id, name) {
      this.isEdit = true
      this.categoryForm.id = id
      this.categoryForm.cname = name
    },
    handleDelete(id) {
      this.$axios({
        method: "delete",
        url: '/admin/deleteCategory',
        params: {
          id
        }
      }).then(res => {
        if (res.data.code == 2000) {
          alert('删除成功')
          this.findAll()
        } else alert('删除失败')
      })
    },
    findAll() {
      this.$axios({
        method: 'get',
        url: '/category/findAll',
      }).then(res => {
        this.category = res.data.data.data
      })
    },
    submitForm() {
      this.$axios({
        method: 'post',
        url: '/admin/updateCategory',
        data: this.categoryForm
      }).then(res => {
        if (res.data.code == 2000) {
          alert('修改成功')
          this.findAll()
        } else {
          alert('修改失败')
        }
      })
    },
    resetForm() {
      this.categoryForm.cname = ''
    }
  }
}
</script>

<style scoped>

</style>
